<template>
  <el-button plain @click="dialogVisible = true"> Click to open the Dialog </el-button>

  <el-dialog
    v-model="dialogVisible"
    title="请完成安全验证"
    width="332"
    :before-close="handleClose"
    class="hci-dialog"
  >
    <div class="hci">
      <div class="hci-panel">
        <div class="hci-panel-bar">
          <div class="hci-panel-bar-item">
            <el-icon><Refresh /></el-icon>
          </div>
        </div>
        <div class="hci-panel-bg">
          <img
            class="yidun_bg-img"
            alt="验证码背景"
            src="https://necaptcha.nosdn.127.net/406c711abc11499c9d87a1726c0432bc.jpg"
          />
        </div>
        <div class="hci-panel-jigsaw" :style="{ left: `${piecePosition.x}px` }">
          <img
            class="yidun_jigsaw"
            alt="验证码滑块"
            src="https://necaptcha.nosdn.127.net/3334b6c3ded34232ab90c5fb54bf3d98.png"
            style="left: 0px"
          />
        </div>
      </div>
      <div class="hci-control">
        <div
          :style="{ left: `${piecePosition.x}px` }"
          class="hci-control-btn"
          @mousedown="onMouseDown"
          @mousemove="onMouseMove"
          @mouseup="onMouseUp"
        >
          <el-icon><DArrowRight /></el-icon>
        </div>
      </div>
    </div>
  </el-dialog>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { ElMessageBox } from 'element-plus'

const dialogVisible = ref(true)
const isDragging = ref(false)
const piecePosition = ref({
  x: 0,
  y: 0,
  currentX: 0,
})

const handleClose = (done: () => void) => {
  ElMessageBox.confirm('Are you sure to close this dialog?')
    .then(() => {
      done()
    })
    .catch(() => {
      // catch error
    })
}

const onMouseDown = (event: MouseEvent) => {
  isDragging.value = true
  piecePosition.value.x = event.clientX - piecePosition.value.currentX
}

const onMouseMove = (event: MouseEvent) => {
  if (!isDragging.value) return
  piecePosition.value.currentX = event.clientX - piecePosition.value.x
}

const onMouseUp = () => {
  isDragging.value = false
}
</script>

<style scoped lang="less">
.hci-dialog {
  .el-dialog {
    .el-dialog__body {
      padding: 0;
    }
  }
}

.hci {
  width: 300px;
  &-panel {
    position: relative;
    height: 150px;
    background-color: #f7f9fa;

    &-bar {
      display: flex;
      align-items: center;
      position: absolute;
      right: 0;
      top: 0;
      height: 30px;
      max-width: 98px;
      z-index: 2;
      background-color: rgba(0, 0, 0, 0.12);

      &-item {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 30px;
        height: 100%;
        cursor: pointer;
        color: #fff;
      }
    }

    &-jigsaw {
      position: absolute;
      top: 0;
    }
  }

  &-control {
    position: relative;
    height: 40px;
    margin-top: 15px;
    position: relative;
    border: 1px solid #e4e7eb;
    background-color: #f7f9fa;
    border-radius: 2px;

    &-btn {
      display: flex;
      align-items: center;
      justify-content: center;
      width: 38px;
      height: 38px;
      position: absolute;
      top: 0;
      left: 0;
      background-color: #fff;
      box-shadow: 0 0 3px rgba(0, 0, 0, 0.3);
      cursor: pointer;
      transition: background 0.2s linear;
    }
  }
}
</style>
